<?php
class pagination{
	var $base_url			= '';
	var $prefix				= '';
	var $suffix				= '';
	var $total_rows			=  0;
	var $per_page			= 10;
	var $num_links			=  2;
	var $cur_page			=  0;
	var $first_link			= '&lsaquo; First';
	var $next_link			= '»';
	var $prev_link			= '«';
	var $last_link			= 'Last &rsaquo;';
	var $full_tag_open		= '';
	var $full_tag_close		= '';
	var $first_tag_open		= '';
	var $first_tag_close	= '';
	var $last_tag_open		= '';
	var $last_tag_close		= '';
	var $first_url			= '';
	var $cur_tag_open		= '';
	var $cur_tag_close		= '';
	var $next_tag_open		= '';
	var $next_tag_close		= '';
	var $prev_tag_open		= '';
	var $prev_tag_close		= '';
	var $num_tag_open		= '';
	var $num_tag_close		= '';
	var $query_string		= 'page';
	var $display_pages		= TRUE;
	var $anchor_class		='';
	public function init($params = array()){
		foreach($params as $key => $var){
			$this->$key=is_array($this->$key)?array_merge($this->$key,$var):$var;
		}
	}
	function create_links(){
		if($this->total_rows==0||$this->per_page==0){return '';}
		$num_pages=ceil($this->total_rows/$this->per_page);
		if($num_pages==1){return '';}
		$this->cur_page=$this->yum->get($this->query_string,true);
		$this->cur_page=(int)$this->cur_page;
		$this->num_links=(int)$this->num_links;
		if ($this->num_links<1){
			$this->yum->error('Your number of links must be a positive number.');
		}
		if (!is_numeric($this->cur_page)){$this->cur_page = 0;}
		if ($this->cur_page>$this->total_rows){
			$this->cur_page=($num_pages- 1)*$this->per_page;
		}
		$uri_page_number=$this->cur_page;
		$this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
		$start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
		$end   = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
		$this->base_url=rtrim($this->base_url).'&amp;'.$this->query_string.'=';
		$output = '';
		if($this->first_link!==FALSE&&$this->cur_page>($this->num_links + 1)){
			$first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
			$output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
		}
		if($this->prev_link!==FALSE&&$this->cur_page!=1){
			$i=$uri_page_number-$this->per_page;
			if ($i == 0&&$this->first_url!=''){
				$output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
			}else{
				$i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
				$output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
			}
		}
		if ($this->display_pages!==FALSE){
			for ($loop = $start -1; $loop <= $end; $loop++){
				$i = ($loop * $this->per_page) - $this->per_page;
				if ($i >= 0){
					if ($this->cur_page==$loop){
						$output .= $this->cur_tag_open.$loop.$this->cur_tag_close;
					}else{
						$n = ($i == 0) ? '' : $i;
						if ($n == ''&&$this->first_url!=''){
							$output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
						}else{
							$n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
							$output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
						}
					}
				}
			}
		}
		if ($this->next_link!==FALSE &&$this->cur_page<$num_pages){
			$output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page * $this->per_page).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
		}
		if ($this->last_link!==FALSE &&($this->cur_page+$this->num_links)<$num_pages){
			$i=(($num_pages * $this->per_page) - $this->per_page);
			$output .= $this->last_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a>'.$this->last_tag_close;
		}
		$output = preg_replace("#([^:])//+#", "\\1/", $output);
		$output = $this->full_tag_open.$output.$this->full_tag_close;
		return $output;
	}
}
?>